/**
 * @program: LeetCode
 * @description: LeetCode : 111. 二叉树的最小深度
 * @author: WXY
 * @create: 2023-01-16 16:31
 * @Version 1.0
 **/
public class Num111_minDepth {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    public int minDepth(TreeNode root) {
        return process(root);
    }

    private int process(TreeNode root) {
        if (root == null) return 0;
        int leftHeight = process(root.left);
        int rightHeight = process(root.right);
        if (root.left == null && root.right != null) {
            return 1 + rightHeight;
        }
        if (root.right == null && root.left != null) {
            return 1 + leftHeight;
        }
        int height = Math.min(leftHeight,rightHeight) +1;
        return height;
    }
}
